package com.djnt.catering.database;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.TreeSet;

import net.sourceforge.pinyin4j.PinyinHelper;
import android.content.ContentValues;
import android.provider.BaseColumns;
import android.text.TextUtils;

public class PinyinColumns implements BaseColumns {
	public static final String NAME = "name";
	protected static final String PINYIN = "pinyin";
	protected static final String FIRST_CHAR = "first_char";
	protected static final String SECOND_CHAR = "second_char";
	protected static final String THIRD_CHAR = "third_char";

	static void setPinyinValues(ContentValues cv, String name) {
		// TODO Auto-generated method stub
		cv.put(NAME, name);
		name = name.replaceAll("[\\x00-\\x20]", "");
		if (TextUtils.isEmpty(name)) {
			return;
		}
		HashSet<Character> first = new HashSet<Character>();
		HashSet<Character> second = new HashSet<Character>();
		HashSet<Character> third = new HashSet<Character>();
		ArrayList<Object> alo = new ArrayList<Object>(name.length());
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);
			String[] py = PinyinHelper.toHanyuPinyinStringArray(c);
			if (py != null && py.length > 0) {
				TreeSet<String> b = new TreeSet<String>();
				for (String a : py) {
					String a1 = a.replaceAll("[0-9]", "");
					b.add(a1);
				}
				for (String a : b) {
					sb.append(a).append(' ');
				}
				b.add(String.valueOf(c));
				alo.add(b.toArray());
			} else {
				alo.add(c);
			}
			sb.append(c).append('\n');
		}
		cv.put(PINYIN, sb.toString());

		for (int i = 0; i < alo.size(); i++) {
			Object s = alo.get(i);
			String ch1 = getNChars(s, 0);
			String ch2 = getNChars(s, 1);
			String ch3 = getNChars(s, 2);
			addNChars(first, ch1);
			if (i > 0) {
				addNChars(second, ch1);
				addNChars(third, ch2);
			}
			addNChars(second, ch2);
			if (i > 1) {
				addNChars(third, ch1);
			}
			addNChars(third, ch3);
		}
		cv.put(FIRST_CHAR, getString(first));
		cv.put(SECOND_CHAR, getString(second));
		cv.put(THIRD_CHAR, getString(third));
	}

	private static void addNChars(HashSet<Character> second, String ch1) {
		// TODO Auto-generated method stub
		if (!TextUtils.isEmpty(ch1)) {
			for (int i = 0; i < ch1.length(); i++) {
				second.add(ch1.charAt(i));
			}
		}
	}

	private static String getString(HashSet<Character> first) {
		// TODO Auto-generated method stub
		Character[] a = new Character[first.size()];
		first.toArray(a);
		Arrays.sort(a);
		StringBuffer sb = new StringBuffer(a.length);
		for (Character c : a) {
			sb.append(c);
		}
		return sb.toString();
	}

	private static String getNChars(Object s, int i) {
		// TODO Auto-generated method stub
		if (s instanceof Character) {
			return getNCharFromChar((Character) s, i);
		}
		if (s instanceof Object[]) {
			return getNCharFromStringArray((Object[]) s, i);
		}
		return null;
	}

	private static String getNCharFromStringArray(Object[] s, int i) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		for (Object a1 : s) {
			String a = (String) a1;
			if (i >= a.length()) {
				continue;
			}
			char ch = a.charAt(i);
			if (Character.isDigit(ch)) {
				continue;
			}
			sb.append(ch);
		}
		return sb.toString();
	}

	private static String getNCharFromChar(Character s, int i) {
		// TODO Auto-generated method stub
		if (i > 0) {
			return null;
		}
		return String.valueOf(s);
	}
}
